package com.py.credit.controller;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import com.py.credit.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.constant.ResourcePath;
import com.py.credit.entity.AuScontent;
import com.py.credit.entity.ShareContentApplyEntity;
import com.py.credit.utils.DateUtil;
import com.py.credit.utils.UuidUtil;
import com.py.framework.core.entity.SysUserEntity;
import com.py.framework.core.entity.WorkFlowBus;
import com.py.framework.core.log.ExceptionLogger;

@RestController
@RequestMapping(value = "/workflow")
public class WorkflowHandleResultController {

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Autowired
	private ContentService contentService;

	@Autowired
	private DissentProcessService dissentProcessService;

	@Autowired
	private AuScontentService auScontentService;

	@Autowired
	private ApplicationService applicationService;

	@Autowired
	private CreditReportService creditReportService;

	@Autowired
	private ShareCustomizeService shareCustomizeService;

	@Autowired
	private ShareContentApplyService shareContentApplyService;

	@Value("${absolutepath}")
	private String absolutepath;

	/**
	 * 根据审批结果修改业务审核状态
	 */

	@RequestMapping(value = "/processHandleResult.do")
	public RestServiceResult<Map<String, Object>> processHandleResult() {
		RestServiceResult<Map<String, Object>> result = new RestServiceResult<Map<String, Object>>();
		RestServiceResult<List<WorkFlowBus>> handledWorkFlowBus = remoteBaseService.getHandledWorkFlowBus();

		if (RestServiceResult.CODE_SUCCESS == handledWorkFlowBus.getCode()) {

			List<WorkFlowBus> list = handledWorkFlowBus.getData();

			if (null != list) {

				for (WorkFlowBus workflow : list) {
					int busType = workflow.getBusType();

					long busId = workflow.getBusId();

					int proStatus = workflow.getProStatus();

					Timestamp finishTime = workflow.getFinishTime();

					String checkStaus = proStatus == 3 ? "P" : "R";
					DateFormat sdf = new SimpleDateFormat(DateUtil.YYYY_MM_DD_HH_MM_SS);
					// 判断业务流程类型:
					switch (busType) {
					case 4:

						contentService.checkCommonContent(String.valueOf(busId), checkStaus);
						break;

					case 5:
						String status = proStatus == 3 ? "Y" : "N";
						dissentProcessService.updateStatus((int) busId, status, sdf.format(finishTime));
						break;

					case 6:

						AuScontent auScontent = new AuScontent();
						auScontent.setCheckStatus(checkStaus);
						auScontent.setId(new Long(busId).intValue());
						auScontent.setAuthKey(UuidUtil.getInstance().getUUid());
						if (finishTime != null) {
							try {
								auScontent.setCheckTime(DateUtil.getInstance().parseDateStr(finishTime.toString(),
										DateUtil.YYYY_MM_DD_HH_MM_SS));
							} catch (ParseException e) {
								e.printStackTrace();
							}
						}

						RestServiceResult<SysUserEntity> lastAssignee = remoteBaseService
								.getLastAssignee(workflow.getProcId());

						if (RestServiceResult.CODE_SUCCESS == lastAssignee.getCode()) {

							SysUserEntity data = lastAssignee.getData();

							if (data != null) {

								auScontent.setCheckUser(data.getUsername());

							}

						}

						auScontentService.update(auScontent);
						break;
					// 共享目录申请
						case 7:
							String applicationStaus = proStatus == 3 ? "Y" : "N";
							String pdfpath="";
							try {
								/*Application application=applicationService.selectApplicationInfo((int) busId);

								if(applicationStaus.equals("Y")){
									creditReportService.createCreditReport(absolutepath,application.getTybm(),application.getApplicant_unit());
									pdfpath = absolutepath + ResourcePath.CREDIT_REPORT_DIR + application.getTybm() + "/" + ResourcePath.CREDIT_REPORT_NAME;
								}

								applicationService.updateApplication(application.getId(),DateUtil.getInstance().parseDateStr(finishTime.toString(),
										DateUtil.YYYY_MM_DD_HH_MM_SS),applicationStaus,pdfpath);*/
								Map<String,String> m=remoteBaseService.getUserInfo((int) busId);

								if(applicationStaus.equals("Y")){
									creditReportService.createCreditReport(absolutepath,m.get("tybm"),m.get("ztmc"));
									pdfpath = absolutepath + ResourcePath.CREDIT_REPORT_DIR + m.get("tybm") + "/" + ResourcePath.CREDIT_REPORT_NAME;
								}

								remoteBaseService.updateCheckReport((int) busId,DateUtil.getInstance().parseDateStr(finishTime.toString(),
										DateUtil.YYYY_MM_DD_HH_MM_SS),applicationStaus,pdfpath);
							} catch (Exception e) {
								ExceptionLogger.error("WorkflowHandleResultController#processHandleResult.do", "定时任务反馈异常！ERROR:" + e.getMessage(), e);
							}
							break;
					case 8:
						String finalStaus = proStatus == 3 ? "Y" : "N";
						RestServiceResult<SysUserEntity> lastAssigneer = remoteBaseService.getLastAssignee(workflow.getProcId());
						Long userId = null;
						if (RestServiceResult.CODE_SUCCESS == lastAssigneer.getCode()) {
							SysUserEntity data = lastAssigneer.getData();
							if (data != null) {
								userId = data.getId();
							}
						}

						try {
							// 更新共享申请状态
							shareContentApplyService.updateShareApply(userId, sdf.format(finishTime), finalStaus, busId);
							// 查询申请信息
							ShareContentApplyEntity entity = shareContentApplyService.shareApplyDetail(busId);
							// 新增共享定制
							shareCustomizeService.addShareCustomize(entity.getTopDeptId(), null, null, "W");
						} catch (Exception e) {
							e.printStackTrace();
						}
						break;
					default:
						break;
					}

					remoteBaseService.updateWorkFlowBusUpdateFlag(String.valueOf(workflow.getId()));

				}
			}

		}

		return result;
	}

}
